#include "MesinhaRacruvido.h"
#include <GL/glu.h>
#include "gllib/Objects.h"
#include "src/mathlib/Matrix4.h"
#include <cmath>

// Variaveis globais:
double topo_lado = 1.0;

double perna_raio = 0.2;
double perna_altura = 1.21;
double perna_precisao = 4;
double perna_razao_intersecao = (2.0)*perna_altura;

// Tampo da mesa:
void draw_tampo(void)
{
    glPushMatrix();
        translationMatrix(Vector3(0.0,1.0,0.0)).applyGL();
        shearMatrix(30).applyGL();
        scaleMatrix(2.5,0.2,3.5).applyGL();
        gllibCube(topo_lado);
    glPopMatrix();
}

// Perna da mesa:
void draw_perna(double x, double z)
{
    glPushMatrix();
    // Posição da perna:
        translationMatrix(Vector3(x,0.0,z)).applyGL();
        // Parte de cima:
        glPushMatrix();
            translationMatrix(Vector3(0.0,perna_altura/perna_razao_intersecao,0.0)).applyGL();
            gllibCone(perna_raio,perna_altura,perna_precisao);
        glPopMatrix();
        // Perte de baixo
        glPushMatrix();
            translationMatrix(Vector3(0.0,-perna_altura/perna_razao_intersecao,0.0)).applyGL();
            rotationMatrix(rad(180),Vector3(1.0,0.0,0.0)).applyGL();
            gllibCone(perna_raio,perna_altura,perna_precisao);
        glPopMatrix();
    glPopMatrix();
}

// Desenhar perna no ponto (0,y,0):
void draw_perna(void){draw_perna(0.0,0.0);}

void MesinhaRacruvido::draw()
{
    // Eixo-Y:
//    gllibEixoY();

    // Subindo um pouco a mesa:
    translationMatrix(Vector3(0.0,1.0,0.0)).applyGL();

    glColor3f(0.2, 0.5, 0.4);
    draw_tampo();

    GLfloat kX = ((float)2/2)*topo_lado;
    GLfloat kZ = ((float)3/2)*topo_lado;

    glColor3f(0.0, 1.0, 1.0);
    draw_perna(kX,kZ);
    draw_perna(kX,-kZ);
    draw_perna(-kX,kZ);
    draw_perna(-kX,-kZ);
}
